<template>
  <div class="com-container">
    <div ref="rank" class="com-chart" />
  </div>
</template>
<script>
// import { getRank } from '../api/sys'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      rankChart: null,
      allData: null,
      timerId: null,
      start: 0,
      end: 9
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.rankChart.dispose() // 销毁当前的图表
      this.initChart() // 更换主题后重新渲染
      this.screenAdaptation() // 屏幕适配
      this.upDataChart() // 更新图表数据
    }
  },
  created() {
    this.$socket.registerCallBack('rankData', this.getData)
  },
  mounted() {
    this.initChart()
    this.$socket.send({
      action: 'getData',
      socketType: 'rankData',
      chartName: 'rank',
      value: ''
    })
    // this.getData()
    window.addEventListener('resize', this.screenAdaptation)
    this.screenAdaptation()
  },
  destroyed() {
    clearInterval(this.timerId)
    window.removeEventListener('resize', this.screenAdaptation)
    this.$socket.unRegisterCallBack('rankData')
  },
  methods: {
    initChart() {
      this.rankChart = this.$eCharts.init(this.$refs.rank, this.theme)
      const initOption = {
        title: {
          text: '▎ 地区销售排行',
          left: 20,
          top: 20
        },
        grid: {
          top: '40%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          type: 'bar',
          itemStyle: {
            barBorderRadius: [22, 22, 0, 0]
          }
        }]
      }
      this.rankChart.setOption(initOption)
      this.rankChart.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.rankChart.on('mouseout', () => {
        this.startSetInterval()
      })
    },
    getData(res) {
      this.allData = res
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.upDataChart()
      this.startSetInterval()
    },
    upDataChart() {
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      const cityNames = this.allData.map(item => {
        return item.name
      })
      const values = this.allData.map(item => {
        return item.value
      })
      const dataOption = {
        xAxis: {
          data: cityNames
        },
        // 开启区间
        dataZoom: {
          show: false,
          startValue: this.start,
          endValue: this.end
        },
        series: [
          {
            data: values,
            itemStyle: {
              // 根据value 大小设备不同颜色渐变
              color: arg => {
                let targetColorArray = []
                if (arg.value > 300) {
                  targetColorArray = colorArr[0]
                } else if (arg.value > 200) {
                  targetColorArray = colorArr[1]
                } else {
                  targetColorArray = colorArr[2]
                }
                return new this.$eCharts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: targetColorArray[0]
                }, {
                  offset: 1,
                  color: targetColorArray[1]
                }])
              }
            }
          }
        ]
      }
      this.rankChart.setOption(dataOption)
      this.startSetInterval()
    },
    startSetInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.start++
        this.end++
        if (this.end > this.allData.length - 1) {
          this.start = 0
          this.end = 9
        }
        this.upDataChart()
      }, 1500)
    },
    screenAdaptation() {
      const titleFontSize = this.$refs.rank.offsetWidth / 100 * 3.6
      const screenOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize / 1.2
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.rankChart.setOption(screenOption)
      this.rankChart.resize()
    }
  }
}
</script>
